﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using Imageo.bintje;
using System.Collections.Specialized;

namespace Imageo
{
    public partial class ImageTable : System.Web.UI.UserControl
    {
        /// <summary>
        /// ImageBox wrapper class. Uses for serialization purposes
        /// </summary>
        class ImageBox
        {
            private int id;
            public int Id
            {
                get { return id; }
                set { id = value; }
            }

            private int trailid;
            public int TrailId
            {
                get { return trailid; }
                set { trailid = value; }
            }

            private string imageUrl;
            public string ImageUrl
            {
                get { return imageUrl; }
                set { imageUrl = value; }
            }

            private string displaySimilarTrailOption;
            public string DisplaySimilarTrailOption
            {
                get { return displaySimilarTrailOption; }
                set { displaySimilarTrailOption = value; }
            }

            private string displaySimilarImageOption;
            public string DisplaySimilarImageOption
            {
                get { return displaySimilarImageOption; }
                set { displaySimilarImageOption = value; }
            }

            private string searchSimilarImageUrl;
            public string SearchSimilarImageUrl
            {
                get { return searchSimilarImageUrl; }
                set { searchSimilarImageUrl = value; }
            }

            private string searchSimilarTrailUrl;
            public string SearchSimilarTrailUrl
            {
                get { return searchSimilarTrailUrl; }
                set { searchSimilarTrailUrl = value; }
            }
        }

        IList<bintje.imageWrapper> images;

        #region Properties
        public Unit Width
        {
            set
            {
                pnlTrailsTableControl.Width = value;
            }
        }

        public Unit Height
        {
            set
            {
                pnlTrailsTableControl.Height = value;
            }
        }
        
        public bool ShowPager
        {
            get
            {
                Control c = ListView1.FindControl("rowPager");
                if (c != null) return c.Visible;
                return false;
            }
            set
            {
                Control c = ListView1.FindControl("rowPager");
                if (c != null) c.Visible=value;
            }
        }

        public IList<bintje.imageWrapper> Images
        {
            get { return images; }
            set { images = value; }
        }

        #endregion

        public void LoadImages(IList<bintje.imageWrapper> imageList)
        {
            images = imageList;

            List<ImageBox> imageBoxes = new List<ImageBox>();

            if (images != null)
            {
                foreach (bintje.imageWrapper iw in images)
                {
                    if (iw.url != null)
                    {
                        ImageBox newBox = new ImageBox();
                        newBox.SearchSimilarImageUrl = ResolveClientUrl(@"~/ui/TrailSearch.aspx?imageid=" + iw.id);
                        newBox.DisplaySimilarImageOption = "display: block;";
                        newBox.SearchSimilarTrailUrl = ResolveClientUrl(@"~/ui/TrailSearch.aspx?imageid=" + iw.id + "&trailid=" + iw.trailId);
                        newBox.DisplaySimilarTrailOption = "display: block;";
                        newBox.Id = iw.id;
                        newBox.ImageUrl = iw.url.Replace("type=image", "type=thumb");

                        imageBoxes.Add(newBox);
                    }
                }
            }

            ListView1.DataSourceID = null;
            ListView1.DataSource = imageBoxes;
            ListView1.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void ListView1_PagePropertiesChanged(object sender, EventArgs e)
        {
            ListView1.DataBind();
        }
    }
}